import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import VueI18n from 'vue-i18n'
import { Toast } from 'vant';
import axios from 'axios'
import 'vant/lib/vant-css/index.css';
import moment from 'moment'; //时间库

Vue.config.productionTip = false

const http = axios.create({
  baseURL: process.env.VUE_APP_APIHOST,
  timeout: 30000,
  withCredentials: true,
});
// 将封装的ajax请求插件写入vue的原型
Vue.prototype.$http = http;

Vue.use(VueI18n)
// Vue.use(Toast);
Vue.prototype.$Toast = Toast;

const i18n = new VueI18n({
  locale: localStorage.language ? localStorage.language : navigator.language.slice(0, 2),  // 语言标识
  messages: {
    'zh': require('./common/lang/zh'),
    'en': require('./common/lang/en')
  }
})

router.beforeEach((to, from, next) => {
  console.log(to);
  let getConfig = async function () {
    const res = await http.get("/getJsConfig", {
      params: {
        url: "http://perrotion.artcare.com" + to.fullPath,
      }
    })
    wx.config({
      debug: true,
      appId: res.data.appId,
      timestamp: parseInt(res.data.timestamp),
      nonceStr: res.data.nonceStr,
      signature: res.data.signature,
      jsApiList: ["onMenuShareTimeline", "onMenuShareAppMessage"]
    });
  };
  let isIOS = function () {
    var isIphone = navigator
      .userAgent
      .includes('iPhone');
    var isIpad = navigator
      .userAgent
      .includes('iPad');
    return isIphone || isIpad;
  };
  console.log(to)
  var config = {
    title: "贝浩登 PERROTIN", // 分享标题
    desc: 'http://perrotion.artcare.com' + to.fullPath, // 分享描述
    link: "http://perrotion.artcare.com/redirect.html?app3Redirect=http://perrotion.artcare.com" + to.fullPath,
    imgUrl: `http://perrotion.artcare.com/favicon.png`,
    type: 'link',
    success: function () { },
    cancel: function () { }
  };
  wx.ready(() => {
    wx.onMenuShareAppMessage(config);
    wx.onMenuShareTimeline(config);
  });
  if (isIOS()) {
    if (sessionStorage.getItem('configed') != 1) {
      sessionStorage.setItem('configed', 1);
      getConfig();
    }
    next();
  } else {
    getConfig();
    next();
  }
});


// 创建时间格式过滤器
Vue.filter('dateformat', (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') => moment(dataStr).format(pattern));

new Vue({
  router,
  store,
  i18n,
  render: h => h(App)
}).$mount('#app')
